package cn.wolfcode.edu.service.impl;

import cn.wolfcode.edu.domain.Employee;
import cn.wolfcode.edu.domain.HanCloud;
import cn.wolfcode.edu.mapper.HanCloudMapper;
import cn.wolfcode.edu.service.IHanCloudService;
import cn.wolfcode.edu.util.UploadUtil;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class HanCloudServiceImpl implements IHanCloudService {

    @Autowired
    private HanCloudMapper hanCloudMapper;

    public List<HanCloud> queryMenusByEmployeeId() {
        //得到当前登录用户的编号
        Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();
        Long employeeId = employee.getId();
        return hanCloudMapper.listAllParentMenus(employeeId);
    }

    public void save(HanCloud hanCloud) {
        //设置当前登录的用户的id
        Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();
        hanCloud.setEmployee(employee);
        //保存到数据库
        hanCloudMapper.insert(hanCloud);
    }

    public void upload(String path, Long parentId,String fileName) {
        HanCloud hanCloud = new HanCloud();
        String url = "/hancloud/download?path=" + path;
        hanCloud.setUrl(url);
        hanCloud.setText(fileName);
        hanCloud.setParentId(parentId);
        //设置当前登录的用户的id
        Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();
        hanCloud.setEmployee(employee);
        //保存到数据库
        hanCloudMapper.insert(hanCloud);

    }

    public void deleteFileById(Long id) {
        //数据库中删除指定文件的信息
        hanCloudMapper.deleteByPrimaryKey(id);

    }
}
